package com.commonly.commonlyUsed.线程;

/**
 *  join
 *
 * */
public class 两个线程依次执行 {

    public static void demo01(){
        Thread A = new Thread(new Runnable() {
            @Override
            public void run() {
                printNumber("A");
            }
        });

        Thread B = new Thread(new Runnable() {
            @Override
            public void run() {
                //希望B在A全部打印完之后在开始打印
                try{
                    A.join();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                printNumber("B");
            }
        });

        A.start();
        B.start();
    }

    private static void printNumber(String threadName){
        int i = 0 ;
        while (i++ < 3){
            try {
                Thread.sleep(100);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println(threadName + " print: " + i );
        }
    }

    public static void main(String[] args) {
        demo01();
    }

}
